#include "stdafx.h"
#include "OptionItem.h"

COptionItem::COptionItem(CListUI* pListUI, CPaintManagerUI* PaintManager)
	: m_pListUI(pListUI)
	, m_PaintManager(PaintManager)
{

}

COptionItem::~COptionItem()
{
}

void COptionItem::InitControl(LPCTSTR lpRegistryKey, LPCTSTR lpItemText, BOOL bShowDesc)
{
	m_pBuilder = new CDialogBuilder;
	CControlUI* pControl = m_pBuilder->Create(_T("option_item.xml"), (UINT)0, NULL, m_PaintManager, this);
	if (pControl != nullptr)
	{
		CLabelUI* pItemText = reinterpret_cast<CLabelUI*>(FindSubControl(_T("item_text")));
		pItemText->SetText(lpItemText);

		BOOL bIsOpenning = GetOptionValueInRegistry(lpRegistryKey);

		CButtonUI* pShowDescButton = dynamic_cast<CButtonUI*>(FindSubControl(_T("show_desc")));
		CButtonUI* pItemButton = reinterpret_cast<CButtonUI*>(FindSubControl(_T("item_btn")));
		if (bIsOpenning)
		{
			pItemButton->SetNormalImage(_T(R"(file='turn_ON.png' source='0,0,45,23')"));
			pItemButton->SetHotImage(_T(R"(file='turn_ON.png' source='0,23,45,46')"));
			pItemButton->SetPushedImage(_T(R"(file='turn_ON.png' source='0,46,540,70')"));
		}
		else
		{
			pItemButton->SetNormalImage(_T(R"(file='turn_OFF.png' source='0,0,45,23')"));
			pItemButton->SetHotImage(_T(R"(file='turn_OFF.png' source='0,23,45,46')"));
			pItemButton->SetPushedImage(_T(R"(file='turn_OFF.png' source='0,46,540,70')"));
		}

		if (bShowDesc)
		{
			pShowDescButton->SetUserData(lpRegistryKey);
			pShowDescButton->SetTag(kOptionDescButton);
		}
		else
		{
			pShowDescButton->SetText(_T(""));
			pShowDescButton->SetMouseEnabled(false);
		}
		
		pItemButton->SetUserData(lpRegistryKey);
		pItemButton->SetTag(kOptionItemButton);

		m_pListUI->Add(pControl);
		this->SetOwner(m_pListUI);
	}
}

BOOL COptionItem::GetOptionValueInRegistry(LPCTSTR lpOptionKey)
{
	CRegKey reg;
	DWORD dwResult = 0;

	if (ERROR_SUCCESS != reg.Open(HKEY_CURRENT_USER, _T("SOFTWARE\\SystemOptimizer")))
	{
		return 0;
	}

	if (ERROR_SUCCESS != reg.QueryDWORDValue(lpOptionKey, dwResult))
	{
		return 0;
	}

	reg.Close();

	return (BOOL)dwResult;
}
